package yl.hs.bmipfull.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.system.ISearchFilterDao;
import yl.hs.bmipfull.daos.system.ISearchViewDao;
import yl.hs.bmipfull.daos.system.ISearchViewUpdateDao;
import yl.hs.bmipfull.pojo.system.SearchFilterEntity;
import yl.hs.bmipfull.pojo.system.SearchViewEntity;
import yl.hs.bmipfull.pojo.system.SearchViewUpdateEntity;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.system.ViewShareTag;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
 * SearchFilterServiceImpl
 * @category
 * @author 谢景广
 * @date 2021-10-23
 */
@Service
public class SearchViewServiceImpl extends SystemBaseService implements ISearchViewService {

    @Autowired
    ISearchFilterDao filterDao;

    @Autowired
    ISearchViewDao viewDao;

    @Autowired
    ISearchViewUpdateDao updateDao;

    private void setValue(Map<String,Object> data,SearchViewEntity entity){
        entity.setMenuSN(data.get("menuSN").toString());
        entity.setTitle(data.get("title").toString());
        entity.setShareType(data.get("shareType").toString());
    }

    @Override
    public Integer saveViewModel(SubmitModel model) throws Exception {
        SearchViewEntity entity;
        var user = HsApp.getCurrent();
        if(Tl.isEmpty(model.getSn())){
            entity = new SearchViewEntity();
            entity.setSn(queryNewSN(entity.getClass()));
            entity.setDisplayFields("");
        }
        else
        {
            entity = viewDao.findBySN(model.getSn());
        }
        setValue(model.getData(),entity);
        List<ViewShareTag> tags = new ArrayList<ViewShareTag>();
        if (entity.getShareType().equals("public")){
            tags.clear();
        }
        else
        {
            tags.add(new ViewShareTag(user.getUserSN(),ViewShareTag.OWNER,user.getUserName()));
            if(model.getData().get("user")!=null) {
                var users = (List<Map<String, String>>) model.getData().get("user");
                if (users.size() > 0) {
                    for (int i = 0; i < users.size(); i++) {
                        var item = users.get(i);
                        tags.add(new ViewShareTag(item.get("sn"),item.get("name")));
                    }
                }
            }
        }
        entity.setTags(Tl.toJson(tags));
        if(Tl.isEmpty(model.getSn())){
            var umv = new SearchViewUpdateEntity(entity.getSn(), user.getUserSN());
            updateDao.insertNew(umv);
            return viewDao.insertNew(entity);
        }
        else
        {
            return viewDao.update(entity);
        }
    }
    private String getDefaultViewSN(SubmitModel model) throws Exception {
        var viewSN = "";
        if(Tl.isEmpty(model.getSn())){
            var user = HsApp.getCurrent();
            var menuSN = model.getData().get("menuSN").toString();
            SearchViewEntity defView = viewDao.findMenuDefaultView(menuSN,user.getUserSN());
            if(defView==null){
                defView = new SearchViewEntity();
                viewSN = queryNewSN(defView.getClass());
                defView.setSn(viewSN);
                var tags = new ArrayList<ViewShareTag>();
                tags.add(new ViewShareTag(user.getUserSN(),ViewShareTag.OWNER,user.getUserName()));
                defView.setTags(Tl.toJson(tags));
                defView.setTitle("默认视图");
                defView.setMenuSN(menuSN);
                defView.setShareType(SearchViewEntity.PRIVATE);
                viewDao.insertNew(defView);
            }
            else
            {
                viewSN = defView.getSn();
            }
        }
        else
        {
            viewSN = model.getSn();
        }
        return viewSN;
    }
    @Override
    public Integer saveViewFilter(SubmitModel model) throws Exception {
        var data = model.getData();
        if(data.get("filters")!=null) {
            var filters = (List<Map<String,String>>)data.get("filters");
            String viewSN=getDefaultViewSN(model);
            filterDao.deleteByViewSN(viewSN);
            if(filters.size()>0){
                int count = 0;
                for (int i = 0; i < filters.size(); i++) {
                    var item = filters.get(i);
                    var field= item.get("field");
                    var operator= item.get("operator");
                    var searchVal= Tl.getString(item.get("searchVal"));
                    var filter = new SearchFilterEntity(queryNewSN(SearchFilterEntity.class),viewSN,"SearchFilterEntity",field,operator,searchVal);
                    count += filterDao.insertNew(filter);
                }
                return count;
            }
        }
        return 0;
    }

    @Override
    public Integer saveViewFields(SubmitModel model) throws Exception {
        var data = model.getData();
        if(data.get("fields")!=null) {
            var fields = (List<String>)data.get("fields");
            var viewSN = getDefaultViewSN(model);
            var view = viewDao.findBySN(viewSN);
            if(fields.size()>0){
                view.setDisplayFields(String.join(",",fields));
            }
            return viewDao.update(view);
        }
        return 0;
    }

    @Override
    public Integer updateUserMenuView(PageQuery query) throws Exception {
        if(!Tl.isEmpty(query.getViewSN())) {
            var viewSN = query.getViewSN();
            var user = HsApp.getCurrent();
            var umv = updateDao.findByUserView(viewSN, user.getUserSN());
            if (umv == null) {
                umv = new SearchViewUpdateEntity(viewSN, user.getUserSN());
                return updateDao.insertNew(umv);
            } else {
                umv.setLastUseView((new Date()).getTime());
                return updateDao.update(umv);
            }
        }
        return  0;
    }

    @Override
    public Integer deleteView(String sn) {
        updateDao.deleteUpdates(sn);
        filterDao.deleteByViewSN(sn);
        return viewDao.deleteBySN(sn);
    }
}
